﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class XiaoShou_FuKuan_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             int BillID = 0;
             if (!this.IsPostBack)
             {
                 if (Request.QueryString["ID"] != null)
                 {
                     int.TryParse(Request.QueryString["ID"], out BillID);
                 }
                 if (BillID > 0)
                 {
                     SqlConnection Conn = new SqlConnection();
                     Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                     Conn.Open();
                     SqlCommand Cmd = new SqlCommand();
                     Cmd.Connection = Conn;
                     Cmd.CommandText = @"SELECT 
                                                dbo.销售到款记录.合同ID, 
                                                dbo.销售到款记录.ID, 
                                                dbo.销售到款记录.收款单号, 
                                                dbo.销售到款记录.付款日期, 
                                                dbo.销售到款记录.收款, 
                                                dbo.销售到款记录.备注, 
                                                dbo.产品销售合同.收货单位ID, 
                                                dbo.产品订货单位.名称, 
                                                dbo.产品销售合同.金额,
                                                dbo.产品销售合同.合同编号
                                        FROM         
                                                dbo.产品销售合同 
                                        INNER JOIN
                                                dbo.销售到款记录 ON 
                                                dbo.产品销售合同.ID = dbo.销售到款记录.合同ID 
                                        INNER JOIN
                                                dbo.产品订货单位 ON 
                                                dbo.产品销售合同.收货单位ID = dbo.产品订货单位.ID
                                        WHERE dbo.销售到款记录.ID=" + BillID;

                     Cmd.CommandType = CommandType.Text;
                     SqlDataReader Reader = Cmd.ExecuteReader();
                     if (Reader.Read())
                     {
                         this.ViewState["BillID"] = BillID;
                         this.GongHuoDanWei.DataBind();

                         this.FaPiaoHao.Text = Reader.GetString(2).Trim();
                         this.FaPiaoHao.ReadOnly = true;
                         this.RiQi.Text = Reader.GetDateTime(3).ToString("yyyy年M月d日");
                         this.RiQi.ReadOnly = true;

                         this.HeTongHao.SelectedIndex=1;
                         //获取合同ID
                         int i = Reader.GetInt32(0);

                         this.BenCiFuKuan.Text = Reader.GetDecimal(4).ToString();                      

                         this.BeiZhu.Text = Reader.GetString(5);
                         this.GongHuoDanWei.Text = Reader.GetString(7);
                         this.YingFuKuan.Text = Reader.GetDecimal(8).ToString();   
                         Reader.Close();
                         SqlCommand cmd1 = new SqlCommand("select SUM([收款])as 到款合计 from [销售到款记录]  where [合同ID]=" + i, Conn);
                         SqlDataReader sda1 = cmd1.ExecuteReader();
                         if (sda1.Read())
                         {
                             try
                             {

                                 Decimal 到款合计 = sda1.GetDecimal(0);
                                 this.YiFuJine.Text = 到款合计.ToString();//已付金额
                                 this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 到款合计).ToString();//未付金额
                             }
                             catch (Exception ex)
                             {
                                 this.YiFuJine.Text = "0.00";
                                 this.WeiFuJine.Text = this.YingFuKuan.Text;
                             }
                         }
                         sda1.Close();
                     }
                     else
                     {
                         Reader.Close();
                     }

                     Conn.Close();
                     Reader.Dispose();
                     Cmd.Dispose();
                     Conn.Dispose();
                 }
                 else
                 {
                     this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                 }
             }
            
        }

        //保存按钮
        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            SqlTransaction Tran = Conn.BeginTransaction();
            Cmd.Transaction = Tran;
            if (this.HeTongHao.Text != "0")
            {
                try
                {
                    if (this.ViewState["BillID"] == null)
                    {
                        this.ViewState["BillID"] = -1;
                    }

                    Cmd.CommandText = "XiaoShouFuKuan_In";
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.Add(new SqlParameter("@收款单号", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FaPiaoHao.Text));
                    Cmd.Parameters.Add(new SqlParameter("@付款日期", SqlDbType.Date, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(this.RiQi.Text)));
                    Cmd.Parameters.Add(new SqlParameter("@合同ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.HeTongHao.Text)));                   
              //     Cmd.Parameters.Add(new SqlParameter("@付款方式", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FuKuan.Text));

                    Cmd.Parameters.Add(new SqlParameter("@收款", SqlDbType.Money, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BenCiFuKuan.Text));                   
                    Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BeiZhu.Text));
                    Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                    Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.ExecuteNonQuery();
                    this.Message.Text = "·保存成功";
                    Tran.Commit();                    
                 
                        
                    
                    
                }
                catch (Exception err)
                {
                    Tran.Rollback();
                    this.Message.Text = err.Message;
                    
                }               
            }
            
            Conn.Close();
            Tran.Dispose();
            Cmd.Dispose();
            Conn.Dispose();

            //如果保存成功则跳转到管理页面
            if (Message.Text == "·保存成功")
            {
                Response.Redirect("XiaoShou_FuKuan_In_Search.aspx");
            }
        }

        //返回按钮
        protected void Button_Modify_Click(object sender, EventArgs e)
        {
            Response.Redirect("XiaoShou_FuKuan_In_Search.aspx");
        }

        //合同号下拉别表变化后的处理逻辑代码
        protected void HeTongHao_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RiQi.Text = "";
            this.GongHuoDanWei.Text = "";     
            this.BeiZhu.Text = "";

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            string cmd_tmp = @"SELECT                dbo.产品销售合同.ID, 
                                                     dbo.产品销售合同.合同编号, 
                                                     dbo.产品订货单位.名称,
                                                     dbo.产品销售合同.金额
                                            FROM     dbo.产品销售合同 INNER JOIN
                                                     dbo.产品订货单位 ON 
                                                     dbo.产品销售合同.收货单位ID = dbo.产品订货单位.ID
                                            WHERE    dbo.产品销售合同.ID = ";
            SqlCommand cmd = new SqlCommand( cmd_tmp + this.HeTongHao.SelectedValue, Conn);
            SqlDataReader sda = cmd.ExecuteReader();            
            if (sda.Read())
            {
               
                String 收货单位 = sda.GetString(2);
                this.GongHuoDanWei.Text = 收货单位;
                Decimal yingfukuan=sda.GetDecimal(3);
                this.YingFuKuan.Text = yingfukuan.ToString();
            }
            sda.Close();

            SqlCommand cmd1 = new SqlCommand("select SUM([收款])as 到款合计 from [销售到款记录]  where [合同ID]=" + this.HeTongHao.SelectedValue, Conn);
            SqlDataReader sda1 = cmd1.ExecuteReader();
            if (sda1.Read())
            {
                try
                {

                    Decimal 到款合计 = sda1.GetDecimal(0);
                    this.YiFuJine.Text=到款合计.ToString();//已付金额
                    this.WeiFuJine.Text = (Decimal.Parse(this.YingFuKuan.Text) - 到款合计).ToString();//未付金额
                }catch (Exception ex)
                {
                    this.YiFuJine.Text = "0.00";
                    this.WeiFuJine.Text = this.YingFuKuan.Text;
                }              
            }
            

            sda1.Close();
            Conn.Close();
        }
    }
}
